﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Nirvana
{
    public sealed class AssetCheckWindow : EditorWindow
    {
        private AssetCheckRule[] assetcheckrules;
        private Editor[] editors;
        private bool[] issucs;
        private Vector2 AssetRulesRect;
        private Vector2 starpos;
        private int index = -1;
        static int MinWidth = 475;
        [MenuItem("Nirvana/Asset Check Rule", false)]
        public static void Init()
        {
            AssetCheckWindow window = (AssetCheckWindow)EditorWindow.GetWindow(typeof(AssetCheckWindow));
            window.Show();
            window.minSize = new Vector2(MinWidth, 475);
        }


        private void OnEnable()
        {
            this.hideFlags = HideFlags.HideAndDontSave;
            var assets = AssetDatabase.FindAssets("t:AssetCheckRule");
            assetcheckrules = new AssetCheckRule[assets.Length];
            editors = new Editor[assets.Length];
            issucs = new bool[assets.Length];

            for (int i = 0; i < assets.Length; i++)
            {
                string guid = assets[i];
                string path = AssetDatabase.GUIDToAssetPath(guid);
                this.assetcheckrules[i] = AssetDatabase.LoadAssetAtPath<AssetCheckRule>(path);
                this.editors[i] = Editor.CreateEditor(this.assetcheckrules[i]);
            }
        }

        private void OnFocus()
        {
            this.Repaint();
        }
        private void OnSelectionChange()
        {
            this.Repaint();
        }
        private void OnGUI()
        {
            GUILayout.Space(10f);
            EditorGUILayout.LabelField("Asset Checker", GUITheme.LabelTitleStyle);
            GUILayout.Space(10f);
            GUILayout.Label("Asset Rules:", GUITheme.TitleStyle);
            this.AssetRulesRect = EditorGUILayout.BeginScrollView(this.AssetRulesRect, GUITheme.ContainerStyle);
            DrawAssetRulesUI();
            EditorGUILayout.EndScrollView();

            EditorGUILayout.Space();
            if (GUILayoutEx.ActionButton("Check Assets"))
            {
                this.CheckAssets();
            }
            List<AssetCheckReportItem> list = new List<AssetCheckReportItem>();
            for (int i = 0; i < this.assetcheckrules.Length; i++)
            {
                AssetCheckRule assetCheckRule = this.assetcheckrules[i];
                list.AddRange(assetCheckRule.Report.Reports);
            }


            EditorGUILayout.LabelField("Reports:(鼠标滚轮滑动)");
            AssetCheckReportItem.Show(list, ref this.starpos, ref this.index);


        }

        private void DrawAssetRulesUI()
        {
            if (this.assetcheckrules.Length == 0 )
            {
                EditorGUILayout.LabelField("There is no asset check rule in the project.");
                return;
            }
            for (int i = 0; i < this.assetcheckrules.Length; i++)
            {
                AssetCheckRule assetCheckRule = this.assetcheckrules[i];
                Editor editor = this.editors[i];
                this.issucs[i] = GUILayoutEx.Title(string.Format("{0} ({1})", assetCheckRule.GetDirectorName(), assetCheckRule.name), this.issucs[i]);
                if (this.issucs[i])
                {
                    GUILayoutEx.BeginContents();
                    editor.DrawDefaultInspector();
                    GUILayoutEx.EndContents();
                }
            }
        }


        private void CheckAssets()
        {
            using (ProgressIndicator progressIndicator = new ProgressIndicator("Check Assets"))
            {
                if (!progressIndicator.Show("Start asset check"))
                {
                    AssetCheckRule[] array = AssetCheckRule.GetAllAssetCheckRule();
                    int num = 0;
                    foreach (AssetCheckRule assetCheckRule in array)
                    {
                        num += assetCheckRule.GetTotalCount();
                    }
                    progressIndicator.SetTotal((float)num);
                    foreach (AssetCheckRule assetCheckRule2 in array)
                    {
                        if (assetCheckRule2.Progress(progressIndicator))
                        {
                            break;
                        }
                    }
                }
            }
        }


    }
}

